package com.ydlclass.channelHandler.handler;

import com.ydlclass.YrpcBootstrap;
import com.ydlclass.transport.message.YrpcResponse;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.Charset;
import java.util.concurrent.CompletableFuture;

/**
 * @author Knife
 */
@Slf4j
public class MySimpleChannelInboundHandler extends SimpleChannelInboundHandler<YrpcResponse> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, YrpcResponse yrpcResponse) throws Exception {

        // 服务提供方，给与的结果
        Object returnValue = yrpcResponse.getBody();

        // todo
        returnValue = returnValue == null ? new Object() : returnValue;

        log.info("我想查看的数据里面{}没有转化String之前",yrpcResponse);
        log.info("我想查看的数据里面{}",returnValue);
        // 从全局挂起的请求中寻找预支匹配的待处理的 completableFuture
        CompletableFuture<Object> completableFuture = YrpcBootstrap.PENDING_REQUEST.get(yrpcResponse.getRequestId());
        completableFuture.complete(returnValue);
        if (log.isDebugEnabled()){
            log.debug("以寻找到编号为{}的completableFuture，处理响应结果",yrpcResponse.getRequestId());
        }
    }
}
